package com.eptok.gw.szcups.cups.busi.impl;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.common.GateWayConstant;
import com.eptok.gw.szcups.common.KeyContainer;
import com.eptok.gw.szcups.cups.busi.CupsBusiInterface;
import com.eptok.gw.szcups.hsm.HsmService;
import com.eptok.gw.szcups.monitor.Monitor;

/**
 * 
 * 签到
 *@author:heqishan, Email:heqishan@gmail.com
 *2013-3-6 下午07:39:36
 */
public class SignInBusiImpl implements CupsBusiInterface {

	public void dobusiness(IOMessage ioMessage) {
		try {
			if(GateWayConstant.FIELD_39.equals(ioMessage.getReqMesField(39))){
				String encryptData = (String)ioMessage.getReqMesField(48);
				String prePinKey = encryptData.substring(0,32);			//加密的PIN密钥
				String prePinKey_mac = encryptData.substring(32,40);	//PIN密钥的验证码
				String preMacKey = encryptData.substring(40,56);		//加密的MAC密钥
				String preMacKey_mac = encryptData.substring(56,64);	//MAC密钥的验证码
				
				HsmService hsmService = new HsmService();
				String hsmPinMessage = hsmService.zpk_zmk2lmk(KeyContainer.getInstance().getSzCupsZmk(), 
						prePinKey,HsmService.HSM_3DESKEY_MODE,prePinKey_mac.substring(0,6));
				String hsmMacMessage = hsmService.zak_zmk2lmk(KeyContainer.getInstance().getSzCupsZmk(), preMacKey, preMacKey_mac.substring(0,6));
				
				KeyContainer.getInstance().setChanZPK(hsmPinMessage);
				KeyContainer.getInstance().setChanZAK(hsmMacMessage);
				
				//签到成功
				Monitor.getInstance().setChanLogin(true);
			}else{
				//TODO 签到失败
				Monitor.getInstance().setChanLogin(false);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Monitor.getInstance().setChanLogin(false);
		}
	}

}
